#include <gtk/gtk.h> 

// 按Enter,获取行编辑的内容;
void enter_callback(GtkWidget *widget,gpointer entry) 
{ 
	const gchar *entry_text; 
	// 获得文本内容;
	entry_text = gtk_entry_get_text(GTK_ENTRY(entry)); 
	g_print("Entry contents: %s\n", entry_text); 
}

int main( int argc, char *argv[] ) 
{ 
	gtk_init(&argc, &argv); 	
 
    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 	
    gtk_window_set_title(GTK_WINDOW(window), "GTK Container"); 		
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); 
	
	// 添加垂直布局,控件均衡摆放,控件间距为5;
	GtkWidget *vbox = gtk_vbox_new(TRUE,5);
	gtk_container_add(GTK_CONTAINER(window),vbox);
	
	// 创建行编辑;
	GtkWidget *entry = gtk_entry_new();
	gtk_container_add(GTK_CONTAINER(vbox),entry);
	// 设置行编辑显示最大字符长度;
	gtk_entry_set_max_length(GTK_ENTRY(entry),100);
	// 设置内容;
	gtk_entry_set_text(GTK_ENTRY(entry),"Hello World");
	// 设置密码模式;
	gtk_entry_set_visibility(GTK_ENTRY(entry),FALSE);
	
	/* 如果我们想在用户输入文本时进行响应，可以为activate设置回调函数;
	 * 当用户在文本输入构件内部按回车键时引发Activate信号;
	 */
	g_signal_connect(entry,"activate",G_CALLBACK(enter_callback),entry);
	
	// 创建水平布局容器;
	GtkWidget *hbox = gtk_hbox_new(TRUE,10);
	gtk_container_add(GTK_CONTAINER(vbox),hbox);
	
	// 创建 button1、button2、button3 并加入到水平布局;
	GtkWidget *button1 = gtk_button_new_with_label("button1");
	gtk_container_add(GTK_CONTAINER(hbox),button1);
	
	GtkWidget *button2 = gtk_button_new_with_label("button2");
	gtk_container_add(GTK_CONTAINER(hbox),button2);
	
	GtkWidget *button3 = gtk_button_new_with_label("button3");
	gtk_container_add(GTK_CONTAINER(hbox),button3);
	
	// 创建关闭按钮,添加到垂直布局;
	GtkWidget *button_close = gtk_button_new_with_label("close");
	gtk_container_add(GTK_CONTAINER(vbox),button_close);
	g_signal_connect(button_close,"clicked",G_CALLBACK(gtk_main_quit),NULL);
	
	gtk_widget_show_all(window);
	
	gtk_main();
	
	return 0;
}
 